package cn.dzhang.spring.boot.model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.hibernate.validator.constraints.Length;

@Entity
@Table(name = "tbl_user")
public class User extends ModelBase {

	private static final long serialVersionUID = 6383312999392742174L;

	@Column(length = 50, unique = true)
	@Length(min = 3, max = 50)
	private String username;
	@Column(length = 50)
	private String password;
	@Column(length = 50, unique = true)
	private String name;
	private boolean online;
	@Enumerated(EnumType.STRING)
	private Gender gender;
	@ManyToMany()
	@JoinTable(name = "tbl_user_role", joinColumns = @JoinColumn(name = "tbl_user_id"), inverseJoinColumns = @JoinColumn(name = "tbl_role_id"))
	private List<Role> roles;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean isOnline() {
		return online;
	}

	public void setOnline(boolean online) {
		this.online = online;
	}

	public Gender getGender() {
		return gender;
	}

	public void setGender(Gender gender) {
		this.gender = gender;
	}

	public List<Role> getRoles() {
		return roles;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	public void addRole(Role role) {
		if (this.roles == null) {
			this.roles = new ArrayList<>();
		}
		this.roles.add(role);
	}

	public void removeRole(Role role) {
		this.roles.remove(role);
	}

	@Override
	public String toString() {
		return String
				.format("User [username=%s, password=%s, name=%s, online=%s, gender=%s]",
						username, password, name, online, gender);
	}

}
